package edu.nju.desserthouse.servlets;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.edu.desserthouse.model.Vip;

import edu.nju.desserthouse.dao.VipDao;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/register.vip")
public class RegisterServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	VipDao vipDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		processRequest (request, response);
	}

	public void processRequest(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		
		String to = "/user/register.jsp";
		RequestDispatcher dispatcher =req.getRequestDispatcher(to);
		String message = "";
		String account;
		String password;
		String retype;
		String name;
		
		int year = 0;
		int month = 0;
		int day = 0;
		
		int gender = 0;
		
		String address;
		String bankAccount;
		
		account = req.getParameter("account");
		password = req.getParameter("password");
		retype = req.getParameter("retype");
		name = req.getParameter("name");
		gender = Integer.valueOf(req.getParameter("gender"));
		address = req.getParameter("address");
		bankAccount = req.getParameter("bankaccoount");	
		String dateString[] = req.getParameter("datepicker").split("/");
		
		try
		{
			year=Integer.valueOf(dateString[2]).intValue();
			month=Integer.valueOf(dateString[0]).intValue()-1;
			day=Integer.valueOf(dateString[1]).intValue();
		}
		catch(Exception e)
		{
			message+="Birthday must be a Integer!<br>";
		}
		
		if(account.equals("")||password.equals("")||retype.equals("")||name.equals("")||address.equals("")||bankAccount.equals("")||!(password.equals(retype))){
			message+="You should fill all the blanks correctly!<br>";
		}
		Vip vip = new Vip();
		vip.setIdByDate();
		vip.setAccount(account);
		vip.setPassword(password);
		vip.setName(name);
		vip.setAddr(address);
		vip.setBankaccount(bankAccount);
		vip.setBalance(0);
		vip.setGender(gender);
		vip.setLevel(0);
		vip.setState(3);
		vip.setTotalconsume(0);
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.YEAR,year);
		calendar.set(Calendar.MONTH,month);
		calendar.set(Calendar.DAY_OF_MONTH,day);
		Date date=new Date(calendar.getTimeInMillis());
		Date now = new Date(new java.util.Date().getTime());
		vip.setBirth(date);
		vip.setJoindate(now);
		vip.setLastpay(null);
		
		try {
			vipDao.save(vip);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		req.getSession().setAttribute("vip_id",vip.getId());
		req.getSession().setAttribute("vip_account",vip.getAccount());
		req.getSession().setAttribute("vip_balance", vip.getBalance());
		req.getSession().setAttribute("vip_level", vip.getLevel());
		resp.sendRedirect("/desserthouseWEB/user/newuser.jsp");
	}

}
